<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>编程题_实现一个flat函数</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
  </style>
</head>

<body>
  <div></div>
</body>
<script>
  // 设计一个flat函数将如下数组arr=[1,2,['3',4,'5',[6,[7,8],9]]]输出为1,2,'3',4,'5',6,7,8,9。至少写出两种方法,要求不能改变数组中的原始数据类型
  "use strict"; {
    // 方法一:
    let arr = [1, 2, ['3', 4, '5', [6, [7, 8], 9]]]
    let res = JSON.stringify(arr).replace(/\[|\]/ig, "")
    console.log(res)
    // 方法二:
    let str = ""
    let forArr = arr => {
      for (let v of arr) {
        if (typeof v == 'object') {
          forArr(v)
        } else {
          str += typeof v == "number" ? `${v},` : `"${v}",`
        }
      }
    }
    forArr(arr)
    str = str.substr(0, str.length - 1)
    console.log(str)
  };
</script>

</html>